package com.jevons.geo_lib.geopackage;

import java.util.ArrayList;
import java.util.List;

import mil.nga.geopackage.GeoPackage;
import mil.nga.geopackage.features.user.FeatureCursor;
import mil.nga.geopackage.features.user.FeatureDao;
import mil.nga.geopackage.features.user.FeatureRow;

/**
 * Copyright 2020 JxGIS
 *
 * @file FeatureGeoOperationi
 * @auth linaisheng
 * Created on 2021/8/11.
 * Description：
 */
public class FeatureGeoOperation {



    private FeatureDao mTableDao;


    private GeoPackage mGeoPackage;

    public FeatureGeoOperation(GeoPackage geoPackage, String tableName) {
        this.mGeoPackage = geoPackage;

        if (mGeoPackage != null) {
            mTableDao = mGeoPackage.getFeatureDao(tableName);
        }
    }


    public FeatureCursor queryAll() {
        if (mTableDao != null) {
            FeatureCursor cursor = mTableDao.queryForAll();
            return cursor;
        }
        return null;
    }

    public FeatureRow queryFeatureById(String featureId) {
        if (mTableDao != null) {
            FeatureCursor cursor = mTableDao.queryForEq(
                    "id", featureId);
            if (cursor.moveToNext()) {
                FeatureRow row = cursor.getRow();
                return row;
            }
        }
        return null;
    }

    public FeatureCursor queryFeatureCursor(String whereStr) {
        if (mTableDao != null) {
            FeatureCursor cursor = mTableDao.query(whereStr);
            return cursor;
        }
        return null;
    }


    public List<FeatureRow> queryFeatureList(String whereStr) {
        if (mTableDao != null) {
            List<FeatureRow> data = new ArrayList<>();
            FeatureCursor cursor = mTableDao.query(whereStr);
            try {
                while (cursor.moveToNext()) {
                    FeatureRow row = cursor.getRow();
                    data.add(row);
                }
            } finally {
                cursor.close();
            }
            return data;
        }
        return null;
    }


    public FeatureCursor distinct(String [] columns) {
        if (mTableDao != null) {
            FeatureCursor cursor = mTableDao.query(true, columns);
            return cursor;
        }
        return null;
    }

    public List<String> distinctByColumn(String column) {
        if (mTableDao != null) {
            List<String> data = new ArrayList<>();
            FeatureCursor cursor = distinct(new String[]{column});
            try {
                while (cursor.moveToNext()) {
                    FeatureRow row = cursor.getRow();
                    String columnValue = row.getValueString(column);
                    data.add(columnValue);
                }
            } finally {
                cursor.close();
            }
            return data;
        }
        return null;
    }




    public boolean updateFeatureRow(FeatureRow row) {
        if (mTableDao != null) {
            boolean result = mTableDao.update(row) > 0;
            return result;
        }
        return false;
    }


    public void closeGeoPackage() {
        if (mGeoPackage != null) {
            mTableDao = null;
            try {
                mGeoPackage.close();
            } catch (Exception e) {

            }
        }
    }


}